//
//  HomeViewController.m
//  BCW
//
//  Created by Mac on 16/3/28.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeV.h"
#import "HBWNetDataManager.h"
#import "LotteryStatusResultModel.h"
#import <MJExtension.h>
#import "DateManager.h"
#import <UIImageView+WebCache.h>

// 屏幕尺寸
#define BCWScreenH [UIScreen mainScreen].bounds.size.height
#define BCWScreenW [UIScreen mainScreen].bounds.size.width

#define TimePlaceholder @"\t"


@interface HomeViewController ()
{
    UIImageView *_headimageView;
    UILabel *_lotteryLabel;
    UIImageView *_NumberimageView;
    HomeV *_homeview;
}

@property (nonatomic, strong) NSTimer   *adTimer;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 创建视图
    [self addChildView];
    
    // loadData
//    [self requestData];
    
    // 通知中心 添加观察者
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endTime) name:EndTimeNoti object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestData) name:@"homeNeedRefresh" object:nil];
    
    // 加载广告图
    [self loadAdvertImg];
}

- (void)loadAdvertImg{
    NSMutableDictionary *param = [[NSMutableDictionary alloc] init];
    param[@"type"] = @"3";
    [NetMgr advertImgWithParam:param Success:^(id responeseObj) {
     
        NSArray *arr =responeseObj[@"data"];
        [self setAdvertViewWithData:arr];
    } failuer:^(NSError *error) {
        
    }];
}

- (void)setAdvertViewWithData:(NSArray *)data{
    
    CGFloat x = 0;
    
    for (NSString *url in data) {
//        NSString *url=data[i];
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(x, 0, _homeview.advertContainer.width, _homeview.advertContainer.height)];
        
        [img sd_setImageWithURL:[NSURL URLWithString:url]];
        [_homeview.advertContainer addSubview:img];
        
        x += _homeview.advertContainer.width;
    }

    if (data.count >= 2) {
        [self advertTimerWithCount:data.count];
    }
    
}

- (void)advertTimerWithCount:(NSInteger)count{
    _adTimer = [NSTimer scheduledTimerWithTimeInterval:3.5 target:self selector:@selector(changeAdOffset:) userInfo:@(count) repeats:YES];
}

- (void)changeAdOffset:(NSTimer *)sender{
//    NSLog(@"%li", [sender.userInfo integerValue]);
    if (_homeview.advertContainer.contentOffset.x / _homeview.advertContainer.width == [sender.userInfo integerValue] - 1) {
        [_homeview.advertContainer setContentOffset:CGPointMake(0, 0) animated:YES];
        return;
        
    }
//    else{
        [_homeview.advertContainer setContentOffset:CGPointMake(_homeview.advertContainer.contentOffset.x + _homeview.advertContainer.width, 0) animated:YES];
//    }

    
//    _homeview.advertContainer.contentOffset = CGPointMake(_homeview.advertContainer.contentOffset.x + _homeview.advertContainer.width, 0);
    
    
}

- (void)endTime{
//    _homeview 
    _homeview.timeL.text = @"正在开奖";
    _homeview.timeL.backgroundColor = [UIColor whiteColor];
    
    // 延迟30秒
    [self performSelector:@selector(requestData) withObject:nil afterDelay:30];
}

- (void)requestData{
    // 关闭用户响应
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [NetMgr homeLotterySuccess:^(id responeseObj) {
        LotteryStatusResultModel *model = [LotteryStatusResultModel mj_objectWithKeyValues:[responeseObj[@"data"] firstObject]];
        
        
        _homeview.model = model;
        [SVProgressHUD dismiss];
        // 通知下一级刷新数据
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NeedReloadData" object:nil];
//        [self beginTimer];
    } failuer:^(NSError *error) {
        
        [SVProgressHUD showErrorWithStatus:@"加载失败!正在退出..."];
        [self performSelector:@selector(exitApp) withObject:nil afterDelay:1.5];
    }];
    
}

- (void)exitApp{
    exit(0);
}

- (void)dealloc{
    
    [_adTimer invalidate];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
//- (void)beginTimer{
//    _timer =  [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeTime:) userInfo:nil repeats:YES];
//}

//- (void)changeTime:(id)sender{
////    _homeview.timeTiele = 
//}

- (void)addChildView{
    //加载视图
    HomeV *homeView = [HomeV homeviewFormXib];
    homeView.frame = CGRectMake(0,  0, BCWScreenW, BCWScreenH);
    [self.view addSubview:homeView];
    _homeview = homeView;    
}


/**
 * 状态栏为白色
 */
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

@end
